package main

import (
	"net/http"
	"gitee.com/Cookie_XiaoD/video_server/api/session"
	"gitee.com/Cookie_XiaoD/video_server/api/defs"
)


const(
	//自定义session头
	HEADER_FIELD_SESSON="X-Session-Id"
	HEADER_FIELD_UNAME="X-User-Name"
)


//验证用户session
//当session未过期时，将username放入请求头
func validateUserSession(r *http.Request) bool {
	sid:=r.Header.Get(HEADER_FIELD_SESSON)
	if len(sid)==0{
		return false
	}
	username,expired:=session.IsSessionExpired(sid)
	if expired{
		return false
	}
	//没有过期
	r.Header.Set(HEADER_FIELD_UNAME,username)
	return true
}

func validateUser(w http.ResponseWriter,r *http.Request)bool{
	uname:=r.Header.Get(HEADER_FIELD_UNAME)
	if len(uname)==0{
		sendErrorResponse(w,defs.ErrorNotAuthUser)
		return false
	}
	return true
}


